package com.ahu.controller;

import com.ahu.Repository.RepairCategoriesRepository;
import com.ahu.Repository.RepairTimePickerRepository;
import com.ahu.Repository.UserInfoRepository;
import com.ahu.annotation.ParamCheck;
import com.ahu.domain.RepairCategories;
import com.ahu.domain.RepairTimePicker;
import com.ahu.domain.UserInfo;
import com.ahu.json.UserInfoJson;
import com.ahu.util.ExceptionManager;
import com.ahu.util.Response;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * QX
 *
 * @author qx
 * @date 2017/11/27
 */
@RestController
@RequestMapping("/v1")
public class UserInfoController {
    private final UserInfoRepository userInfoRepository;
    private final RepairCategoriesRepository repairCategoriesRepository;
    private final ExceptionManager exceptionManager;
    private final RepairTimePickerRepository repairTimePickerRepository;
    @Autowired
    public UserInfoController(UserInfoRepository userInfoRepository, RepairCategoriesRepository repairCategoriesRepository, ExceptionManager exceptionManager, RepairTimePickerRepository repairTimePickerRepository) {
        this.userInfoRepository = userInfoRepository;
        this.repairCategoriesRepository = repairCategoriesRepository;
        this.exceptionManager = exceptionManager;
        this.repairTimePickerRepository = repairTimePickerRepository;
    }




    @ApiOperation(value = "验证用户登录接口", notes = "根据学号与身份证后六位匹配登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idCard", value = "身份证后六位(不包括字母)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "number", value = "用户学号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/userinfo", method = RequestMethod.GET)
    public Response userInfoGet(@ParamCheck String idCard, @ParamCheck String number){
        //根据学号查询用户信息
        UserInfo userInfo = userInfoRepository.findByNumber(number);
        if (userInfo!=null){
            if (Objects.equals(userInfo.getStatus(), "未启用")){
                userInfo.setStatus("已启用");
                userInfo.setUpdateTime(new Date());
                userInfoRepository.save(userInfo);
            } else if (Objects.equals(userInfo.getStatus(), "封禁")){
                throw exceptionManager.createException("1003");
            }
            String id = userInfo.getIdCard().substring(12,18);
            if(Objects.equals(id, idCard)){
                UserInfoJson userInfoJson = new UserInfoJson(userInfo);
                return new Response().success(userInfoJson);
            }
            else if(!Objects.equals(userInfo.getIdCard(), idCard)) {
                throw exceptionManager.createException("1002");
            }
        }
        throw exceptionManager.createException("1001");
    }

    @ApiOperation(value = "报修分类接口")
    @RequestMapping(value = "/repaircategories")
    public Response repairCategoriesGet(){
        List<RepairCategories> repairCategoriesList = repairCategoriesRepository.findAll();
        return new Response().success(repairCategoriesList);
    }

    @ApiOperation(value = "报修时间分类")
    @RequestMapping(value = "/repairtime")
    public Response repairTimeGet(){
        List<RepairTimePicker> repairTimePickerList = repairTimePickerRepository.findAll();
        return  new Response().success(repairTimePickerList);
    }


}
